<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="设备状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择设备状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.device_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备编号" prop="number">
        <el-input
          v-model="queryParams.number"
          placeholder="请输入设备编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备类型" prop="equipmentId">
        <el-select
          v-model="queryParams.equipmentId"
          placeholder="请选择设备类型"
          clearable
        >
          <el-option
            v-for="item in devicetypeList"
            :key="item.id"
            :label="item.tyep"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="设备厂家" prop="companyId">
        <el-select
          v-model="queryParams.companyId"
          placeholder="请选择设备厂家"
          clearable
        >
          <el-option
            v-for="item in manufacturerList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="链接类型" prop="linkType">
        <el-select
          v-model="queryParams.linkType"
          placeholder="请选择链接类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.collection_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="应用位置" prop="location">
        <el-select
          v-model="queryParams.location"
          placeholder="请选择应用位置"
          clearable
        >
          <el-option
            v-for="item in wardList"
            :key="item.id"
            :label="item.wardnum"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="检查科室" prop="checkRoom">
        <el-select
          v-model="queryParams.checkRoom"
          placeholder="请选择检查科室"
          clearable
        >
          <el-option
            v-for="item in departmentList"
            :key="item.id"
            :label="item.name"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="操作人员" prop="operateId">
        <el-select
          v-model="queryParams.operateId"
          placeholder="请选择操作人员"
          clearable
        >
          <el-option
            v-for="item in userList"
            :key="item.id"
            :label="item.userName"
            :value="item.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="在线时长" prop="time">
        <el-input
          v-model="queryParams.time"
          placeholder="请输入在线时长"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检查报警次数" prop="reversoContext">
        <el-input
          v-model="queryParams.reversoContext"
          placeholder="请输入检查报警次数"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="添加时间" prop="starttime">
        <el-date-picker
          clearable
          v-model="queryParams.starttime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择添加时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['device:device:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['device:device:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['device:device:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['device:device:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="deviceList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备id" align="center" prop="id" />
      <el-table-column label="设备状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.device_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column label="设备编号" align="center" prop="number" />
      <el-table-column
        label="设备图片"
        align="center"
        prop="picture"
        width="100"
      >
        <template slot-scope="scope">
          <image-preview :src="scope.row.picture" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="设备类型" align="center" prop="devicetype.tyep">
      </el-table-column>
      <el-table-column label="设备厂家" align="center" prop="manufacturer.name">
      </el-table-column>
      <el-table-column label="链接类型" align="center" prop="linkType">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.collection_status"
            :value="scope.row.linkType"
          />
        </template>
      </el-table-column>
      <el-table-column label="应用位置" align="center" prop="ward.wardnum">
      </el-table-column>
      <el-table-column label="检查科室" align="center" prop="department.name">
      </el-table-column>
      <el-table-column label="操作人员" align="center" prop="sysUser.userName">
      </el-table-column>
      <el-table-column label="在线时长" align="center" prop="time" />
      <el-table-column
        label="检查报警次数"
        align="center"
        prop="reversoContext"
      />
      <el-table-column
        label="添加时间"
        align="center"
        prop="starttime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.starttime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['device:device:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['device:device:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择设备状态">
            <el-option
              v-for="dict in dict.type.device_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备编号" prop="number">
          <el-input v-model="form.number" placeholder="请输入设备编号" />
        </el-form-item>
        <el-form-item label="设备图片" prop="picture">
          <image-upload v-model="form.picture" />
        </el-form-item>
        <el-form-item label="设备类型" prop="equipmentId">
          <el-select v-model="form.equipmentId" placeholder="请选择设备类型">
            <el-option
              v-for="item in devicetypeList"
              :key="item.id"
              :label="item.tyep"
              :value="parseInt(item.id)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备厂家" prop="companyId">
          <el-select v-model="form.companyId" placeholder="请选择设备厂家">
            <el-option
              v-for="item in manufacturerList"
              :key="item.id"
              :label="item.name"
              :value="parseInt(item.id)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="链接类型" prop="linkType">
          <el-select v-model="form.linkType" placeholder="请选择链接类型">
            <el-option
              v-for="dict in dict.type.collection_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="应用位置" prop="location">
          <el-select v-model="form.location" placeholder="请选择应用位置">
            <el-option
              v-for="item in wardList"
              :key="item.id"
              :label="item.wardnum"
              :value="parseInt(item.id)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="检查科室" prop="checkRoom">
          <el-select v-model="form.checkRoom" placeholder="请选择检查科室">
            <el-option
              v-for="item in departmentList"
              :key="item.id"
              :label="item.name"
              :value="parseInt(item.id)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作人员" prop="operateId">
          <el-select v-model="form.operateId" placeholder="请选择操作人员">
            <el-option
              v-for="item in userList"
              :key="item.userId"
              :label="item.userName"
              :value="parseInt(item.userId)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="在线时长" prop="time">
          <el-input v-model="form.time" placeholder="请输入在线时长" />
        </el-form-item>
        <el-form-item label="检查报警次数" prop="reversoContext">
          <el-input
            v-model="form.reversoContext"
            placeholder="请输入检查报警次数"
          />
        </el-form-item>
        <el-form-item label="添加时间" prop="starttime">
          <el-date-picker
            clearable
            v-model="form.starttime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择添加时间"
          >
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="逻辑删除" prop="del">
          <el-input v-model="form.del" placeholder="请输入逻辑删除" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listDevice,
  getDevice,
  delDevice,
  addDevice,
  updateDevice,
} from "@/api/device/device";
import { listManufacturer } from "@/api/device/manufacturer";
import { listDevicetype } from "@/api/device/devicetype/devicetype";
import { listDepartment } from "@/api/department/department";
import { listUser } from "@/api/system/user";
import { listWard } from "@/api/ward/ward";

export default {
  name: "Device",
  dicts: [
    "device_status",
    "sys_job_status",
    "collection_status",
    "collection_status",
  ],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备列表表格数据
      deviceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        status: null,
        number: null,
        equipmentId: null,
        companyId: null,
        linkType: null,
        location: null,
        checkRoom: null,
        operateId: null,
        time: null,
        reversoContext: null,
        starttime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},

      // 设备厂家列表
      manufacturerList: [],

      // 设备类型列表
      devicetypeList: [],

      // 检查科室列表
      departmentList: [],

      // 应用位置列表
      wardList: [],

      // 操作人员列表
      userList: [],
    };
  },
  // 页面一加载 立即执行的函数
  created() {
    this.getList();
    this.getManufacturerList();
    this.getDeviceTypeList();
    this.getDepartMentList();
    this.getUserList();
    this.getWardList();
  },
  methods: {
    /** 查询ward列表 */
    getWardList() {
      this.loading = true;
      listWard(this.queryParams).then((response) => {
        this.wardList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 查询用户列表 */
    getUserList() {
      this.loading = true;
      listUser(this.addDateRange(this.queryParams, this.dateRange)).then(
        (response) => {
          this.userList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },

    /** 查询科室管理列表 */
    getDepartMentList() {
      this.loading = true;
      listDepartment(this.queryParams).then((response) => {
        this.departmentList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 查询设备类型表列表 */
    getDeviceTypeList() {
      this.loading = true;
      listDevicetype(this.queryParams).then((response) => {
        this.devicetypeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 查询设备厂家列表 */
    getManufacturerList() {
      this.loading = true;
      listManufacturer(this.queryParams).then((response) => {
        this.manufacturerList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    /** 查询设备列表列表 */
    getList() {
      this.loading = true;
      listDevice(this.queryParams).then((response) => {
        this.deviceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        status: null,
        number: null,
        picture: null,
        equipmentId: null,
        companyId: null,
        linkType: null,
        location: null,
        checkRoom: null,
        operateId: null,
        time: null,
        reversoContext: null,
        starttime: null,
        del: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
      this.getManufacturerList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getDevice(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改设备列表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateDevice(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
              this.getManufacturerList();
              this.getDeviceTypeList();
              this.getDepartMentList();
              this.getUserList();
              this.getWardList();
            });
          } else {
            addDevice(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              this.getManufacturerList();
              this.getDeviceTypeList();
              this.getDepartMentList();
              this.getUserList();
              this.getWardList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除设备列表编号为"' + ids + '"的数据项？')
        .then(function () {
          return delDevice(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "device/device/export",
        {
          ...this.queryParams,
        },
        `device_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
